package top.wilbert.collegelink.forum.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.wilbert.collegelink.common.constant.CommentConstant;
import top.wilbert.collegelink.common.enums.TargetTypeEnum;
import top.wilbert.collegelink.common.utils.PageUtils;
import top.wilbert.collegelink.common.utils.Query;

import top.wilbert.collegelink.common.vo.UserSimpleInfoVo;
import top.wilbert.collegelink.forum.controller.ThumbController;
import top.wilbert.collegelink.forum.dao.CommentDao;
import top.wilbert.collegelink.forum.dao.TalkDao;
import top.wilbert.collegelink.forum.entity.CommentEntity;
import top.wilbert.collegelink.forum.entity.TalkEntity;
import top.wilbert.collegelink.forum.feign.UserFeignService;
import top.wilbert.collegelink.forum.service.CommentService;
import top.wilbert.collegelink.forum.service.TalkService;
import top.wilbert.collegelink.forum.service.ThumbService;
import top.wilbert.collegelink.forum.vo.CommentCreateVo;
import top.wilbert.collegelink.forum.vo.CommentResponseVo;

@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, CommentEntity> implements CommentService {

    @Autowired
    private TalkDao talkDao;

    @Autowired
    private TalkService talkService;

    @Autowired
    private ThumbService thumbService;

    @Autowired
    private UserFeignService userFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CommentEntity> page = this.page(
                new Query<CommentEntity>().getPage(params),
                new QueryWrapper<CommentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void comment(CommentCreateVo commentCreateVo) {
        //如果有父评论，父评论的子评论数量+1
        if (commentCreateVo.getPid() != 0) {
            CommentEntity commentEntity = baseMapper.selectById(commentCreateVo.getPid());
            commentEntity.setCommentCount(commentEntity.getCommentCount() + 1);
            baseMapper.updateById(commentEntity);
        }

        CommentEntity commentEntity = new CommentEntity();
        BeanUtils.copyProperties(commentCreateVo, commentEntity);
        Date date = new Date();
        commentEntity.setCreateTime(date);
        baseMapper.insert(commentEntity);

        if (commentEntity.getTargetType() == TargetTypeEnum.TALK.getTargetType()) {
            TalkEntity talkEntity = talkDao.selectById(commentEntity.getTargetId());
            //话题评论数量+1
            talkEntity.setCommentCount(talkEntity.getCommentCount() + 1);
            //同时修改话题最后评论时间
            talkEntity.setLatestCommentTime(date);
            talkDao.updateById(talkEntity);
        }

    }

    @Override
    public PageUtils listLevel1ByTalkId(IPage<CommentResponseVo> page, Long targetId, Integer targetType, Long userId) {
        List<CommentResponseVo> commentResponseVos = baseMapper.selectLevel1CommentResponseVo(page, targetId, targetType);
        List<Long> userIds = new ArrayList<>();
        for (CommentResponseVo commentResponseVo : commentResponseVos) {
            userIds.add(commentResponseVo.getCommentatorId());
            Boolean like = thumbService.isLike(commentResponseVo.getCommentId(), TargetTypeEnum.COMMENT.getTargetType(), userId);
            commentResponseVo.setIsLike(like);
        }

        Map<Long, UserSimpleInfoVo> map = userFeignService.searchUserByIds(userIds);

        for (CommentResponseVo commentResponseVo : commentResponseVos) {
            commentResponseVo.setUserSimpleInfoVo(map.get(commentResponseVo.getCommentatorId()));
        }

        IPage<CommentResponseVo> iPage = page.setRecords(commentResponseVos);

        return new PageUtils(iPage);
    }

    @Override
    public PageUtils listLevel2ByTalkId(IPage<CommentResponseVo> page, Long pid, Long userId) {
        List<CommentResponseVo> commentResponseVos = baseMapper.selectLevel2CommentResponseVo(page, pid);
        List<Long> userIds = new ArrayList<>();
        for (CommentResponseVo commentResponseVo : commentResponseVos) {
            userIds.add(commentResponseVo.getCommentatorId());
            userIds.add(commentResponseVo.getReplyId());
            Boolean like = thumbService.isLike(commentResponseVo.getCommentId(), TargetTypeEnum.COMMENT.getTargetType(), userId);
            commentResponseVo.setIsLike(like);
        }

        Map<Long, UserSimpleInfoVo> map = userFeignService.searchUserByIds(userIds);

        for (CommentResponseVo commentResponseVo : commentResponseVos) {
            commentResponseVo.setUserSimpleInfoVo(map.get(commentResponseVo.getCommentatorId()));
            if (commentResponseVo.getType() == CommentConstant.REPLY_LEVEL2_TYPE) {
                commentResponseVo.setReplyUserVo(map.get(commentResponseVo.getReplyId()));
            }
        }

        IPage<CommentResponseVo> iPage = page.setRecords(commentResponseVos);

        return new PageUtils(iPage);
    }

    @Override
    public void addLikeCount(Long commentId) {
        baseMapper.addLikeCount(commentId);
    }

    @Override
    public void reduceLikeCount(Long commentId) {
        baseMapper.reduceLikeCount(commentId);
    }

    @Override
    public void reduceCommentCount(Long commentId) {
        CommentEntity commentEntity = baseMapper.selectById(commentId);
        Integer targetType = commentEntity.getTargetType();
        if (targetType == TargetTypeEnum.TALK.getTargetType()) {
            talkService.reduceCommentCount(commentEntity.getTargetId());
        }
    }

}